package cn.fql.org.controller;
import cn.fql.basic.util.PageList;
import cn.fql.org.query.EmployeeQuery;
import cn.fql.basic.util.AjaxResult;
import cn.fql.org.domain.Employee;
import cn.fql.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.List;

@RestController
@RequestMapping("/employee")
//接口文档的注解
@Api(value = "员工的API",description="员工相关的CRUD功能")
public class EmployeeController {

    @Autowired
    public IEmployeeService employeeService;


    /**
     * 接口：查询单个对象
     */

    /**
     *写一个接口/方法：1.请求方式 2.请求参数 3.请求地址 4.返回数据格式
     * Get http://localhost/employee/1
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "员工的单个对象查询")
    public Employee loadById(@PathVariable("id") Long id){

        return employeeService.loadById(id);
    }
    /**
     * 接口：查询所有
     * Get http://localhost/employee
     */
    @GetMapping()
    @ApiOperation(value = "员工的全部对象查询")
    public List<Employee> loadAll(){
        return employeeService.loadAll();
    }
    /**
     * 分页查询
     * post http://localhost/employee 对象
     */
    @PostMapping
    @ApiOperation(value = "员工分页")
    public PageList<Employee> queryPage(@RequestBody EmployeeQuery employeeQuery){
        return employeeService.queryPage(employeeQuery);
    }

    /**
     * 删除接口
     */
    //delete http://localhost/employee/1
    @DeleteMapping("/{id}")
    @ApiOperation(value = "员工的单个对象删除")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            employeeService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    /**
     * 添加和修改接口
     */
    //put http://localhost/employee {对象}
    @PutMapping
    @ApiOperation(value = "员工的增加或修改")
    public AjaxResult addOrUpdate(@RequestBody Employee employee){
        try {
            if (employee.getId() == null){
                employeeService.save(employee);
            }else{
                employeeService.update(employee);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }
}
